home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Metalworks / PropertiesMetalTheme.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  4.8 KB  |  180 lines

  1. /*
  2.  * @(#)PropertiesMetalTheme.java    1.3 98/08/26
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. import javax.swing.plaf.*;
  17. import javax.swing.plaf.metal.*;
  18. import javax.swing.*;
  19. import javax.swing.border.*;
  20. import java.awt.*;
  21. import java.io.*;
  22. import java.util.*;
  23.  
  24. /**
  25.  * This class allows you to load a theme from a file.
  26.  * It uses the standard Java Properties file format.
  27.  * To create a theme you provide a text file which contains
  28.  * tags corresponding to colors of the theme along with a value
  29.  * for that color.  For example:
  30.  *
  31.  * name=My Ugly Theme
  32.  * primary1=255,0,0
  33.  * primary2=0,255,0
  34.  * primary3=0,0,255
  35.  *
  36.  * This class only loads colors from the properties file,
  37.  * but it could easily be extended to load fonts -  or even icons.
  38.  *
  39.  * @version 1.3 08/26/98
  40.  * @author Steve Wilson
  41.  */
  42. public class PropertiesMetalTheme extends DefaultMetalTheme {
  43.  
  44.     private String name = "Custom Theme";
  45.  
  46.     private ColorUIResource primary1;
  47.     private ColorUIResource primary2;
  48.     private ColorUIResource primary3;
  49.  
  50.     private ColorUIResource secondary1;
  51.     private ColorUIResource secondary2;
  52.     private ColorUIResource secondary3;
  53.  
  54.     private ColorUIResource black;
  55.     private ColorUIResource white;
  56.  
  57.  
  58.     /**
  59.       * pass an inputstream pointing to a properties file.
  60.       * Colors will be initialized to be the same as the DefaultMetalTheme,
  61.       * and then any colors provided in the properties file will override that.
  62.       */
  63.     public PropertiesMetalTheme( InputStream stream ) {
  64.         initColors();
  65.         loadProperties(stream);
  66.     }
  67.  
  68.     /**
  69.       * Initialize all colors to be the same as the DefaultMetalTheme.
  70.       */
  71.     private void initColors() {
  72.         primary1 = super.getPrimary1();
  73.         primary2 = super.getPrimary2();
  74.         primary3 = super.getPrimary3();
  75.  
  76.         secondary1 = super.getSecondary1();
  77.         secondary2 = super.getSecondary2();
  78.         secondary3 = super.getSecondary3();
  79.  
  80.     black = super.getBlack();
  81.     white = super.getWhite();
  82.     }
  83.  
  84.     /**
  85.       * Load the theme name and colors from the properties file
  86.       * Items not defined in the properties file are ignored
  87.       */
  88.     private void loadProperties(InputStream stream) {
  89.     Properties prop = new Properties();
  90.     try {
  91.         prop.load(stream);
  92.     } catch (IOException e) {
  93.         System.out.println(e);
  94.     }
  95.  
  96.     Object tempName = prop.get("name");
  97.     if (tempName != null) {
  98.         name = tempName.toString();
  99.     }
  100.  
  101.     Object colorString = null;
  102.  
  103.     colorString = prop.get("primary1");
  104.     if (colorString != null){
  105.         primary1 = parseColor(colorString.toString());
  106.     }
  107.  
  108.     colorString = prop.get("primary2");
  109.     if (colorString != null) {
  110.         primary2 = parseColor(colorString.toString());
  111.     }
  112.  
  113.     colorString = prop.get("primary3");
  114.     if (colorString != null) {
  115.         primary3 = parseColor(colorString.toString());
  116.     }
  117.  
  118.     colorString = prop.get("secondary1");
  119.     if (colorString != null) {
  120.         secondary1 = parseColor(colorString.toString());
  121.     }
  122.  
  123.     colorString = prop.get("secondary2");
  124.     if (colorString != null) {
  125.         secondary2 = parseColor(colorString.toString());
  126.     }
  127.  
  128.     colorString = prop.get("secondary3");
  129.     if (colorString != null) {
  130.         secondary3 = parseColor(colorString.toString());
  131.     }
  132.  
  133.     colorString = prop.get("black");
  134.     if (colorString != null) {
  135.         black = parseColor(colorString.toString());
  136.     }
  137.  
  138.     colorString = prop.get("white");
  139.     if (colorString != null) {
  140.         white = parseColor(colorString.toString());
  141.     }
  142.  
  143.     }
  144.  
  145.     public String getName() { return name; }
  146.  
  147.     protected ColorUIResource getPrimary1() { return primary1; }
  148.     protected ColorUIResource getPrimary2() { return primary2; }
  149.     protected ColorUIResource getPrimary3() { return primary3; }
  150.  
  151.     protected ColorUIResource getSecondary1() { return secondary1; }
  152.     protected ColorUIResource getSecondary2() { return secondary2; }
  153.     protected ColorUIResource getSecondary3() { return secondary3; }
  154.  
  155.     protected ColorUIResource getBlack() { return black; }
  156.     protected ColorUIResource getWhite() { return white; }
  157.  
  158.     /**
  159.       * parse a comma delimited list of 3 strings into a Color
  160.       */
  161.     private ColorUIResource parseColor(String s) {
  162.         int red = 0;
  163.     int green = 0;
  164.     int blue = 0;
  165.     try {
  166.         StringTokenizer st = new StringTokenizer(s, ",");
  167.  
  168.         red = Integer.parseInt(st.nextToken());
  169.         green = Integer.parseInt(st.nextToken());
  170.         blue = Integer.parseInt(st.nextToken());
  171.  
  172.     } catch (Exception e) {
  173.         System.out.println(e);
  174.         System.out.println("Couldn't parse color :" + s);
  175.     }
  176.  
  177.     return new ColorUIResource(red, blue, green);
  178.     }
  179. }
  180.